home *** CD-ROM | disk | FTP | other *** search
- Path: colossus.holonet.net!russell
- From: russell@news.mdli.com (Russell Blackadar)
- Newsgroups: comp.lang.c++
- Subject: Re: Strange (to me) notation - little help?
- Date: 4 Jan 1996 22:06:43 GMT
- Organization: HoloNet National Internet Access System: 510-704-1058/modem
- Message-ID: <4chj1j$384@colossus.holonet.net>
- References: <Pine.SOL.3.91.960103225557.22729H-100000@qew.cs>
- NNTP-Posting-Host: jubal.mdli.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Rimon Barr (barr@vis.toronto.edu) wrote:
- : > SimpleCat::SimpleCat(int age, int weight):
- : > itsAge(age), itsWeight(weight) {}
-
- : Well, I haven't run the code and I never knew you could do that, but it's
- : is cool and I think I know what it does. Please check this out and get
- : back to me.
-
- There's no mystery here -- it's a member-initialization list.
- See any C++ book.
-
- : In C++ you can call an inherited function as follows:
- : Let's say you have
-
- : class a {
- : a(int num1, int num2);
- : };
- : class b:public a {
- : b(int num1): a(num1, 0);
- ^^^^^^^^^^
- Wrong syntax -- you're missing b::b's function body, which is
- required when you specify a member-initialization list, e.g.
-
- b(int num1): a(num1, 0) { /* something */ }
-
- and of course, you need to make a::a(int, int) public or
- protected for your code to compile.
-
- Perhaps more importantly, you might want to ponder the
- difference between an initialization (which the above is)
- and a function call (which the above is not). Granted, the
- compiler must invoke a::a(int,int), but it may need to do
- other things as well. In fact, it's illegal to call a ctor
- as a function, anyway.
- ...
- : This possess the question of whether the following works too:
- : int a(10);
- : Would the integer a now hold the number 10?
-
- Yes, it would. Why not try it out yourself? Again, this is
- an initialization, not a function call. It means exactly the
- same thing as int a=10;
- --
- Russell Blackadar, russell@mdli.com
-